package cn.stronglink.assetmanage.business.task.presenter;

import java.util.List;

import cn.stronglink.assetmanage.business.task.model.ITaskSyncListener;
import cn.stronglink.assetmanage.business.task.model.OrderType;
import cn.stronglink.assetmanage.business.task.model.TaskSyncModel;
import cn.stronglink.assetmanage.business.task.model.TaskWaitModel;
import cn.stronglink.assetmanage.business.task.view.sync.TaskSyncView;
import cn.stronglink.assetmanage.business.task.view.wait.TaskWaitView;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.utils.ToastUtil;

/**
 * Created by chencongcong on 2018/6/18.
 */

public class TaskSyncPresenter {
    private TaskSyncModel taskSyncModel;
    private TaskSyncView taskSyncView;

    public TaskSyncPresenter(TaskSyncModel taskSyncModel, TaskSyncView taskSyncView) {
        this.taskSyncModel = taskSyncModel;
        this.taskSyncView = taskSyncView;
    }

    /**
     * 初始化未完成数据
     */
    public void initWaitTaskData() {
        List<Task> waitTaskList = taskSyncModel.getSyncList();
        taskSyncView.setTaskListData(waitTaskList);
    }

    // 同步数据
    public void sync() {
        taskSyncModel.setListener(new ITaskSyncListener() {

            @Override
            public void onFail(String orderNo, OrderType orderType, Exception e,int sum) {
                String type = "";
                switch (orderType){
                    case CHECK:
                        type = "验收单";
                        break;
                    case PATROL:
                        type = "巡检单";
                        break;
                    case REPAIR:
                        type = "维修单";
                        break;
                    case INVENTROY:
                        type = "盘点单";
                        break;
                }
                if (sum==0){
                    initWaitTaskData();
                }
                ToastUtil.toastShort(type+"单号为["+orderNo+"]的单子上传失败\r\n" +
                        "错误信息："+e.getMessage());
            }

            @Override
            public void onSucces(String orderNo, OrderType orderType,int sum) {
                String type = "";
                switch (orderType){
                    case CHECK:
                        type = "验收单";
                        break;
                    case PATROL:
                        type = "巡检单";
                        break;
                    case REPAIR:
                        type = "维修单";
                        break;
                    case INVENTROY:
                        type = "盘点单";
                        break;
                }
                if (sum==0){
                    initWaitTaskData();
                }
                ToastUtil.toastShort(type+"单号为["+orderNo+"]的单子上传成功\r\n\r\n还有"+sum+"条数据未上传");
            }
        });
        taskSyncModel.sync();
    }
}
